package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.linkrenhe.frame.http.Tools;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.entity.ComplainInfo;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by Administrator on 2016/12/2 0002.
 */
public class ComplaintCancleActivity extends BaseActivity {

    /*取消投诉的原因文本*/
    private TextView mTvReason;

    /*取消投诉的原因Res*/
    private int[] reasons;

    /*弹出的popupWindow*/
    private PopupWindow popupWindow;

    /*取消投诉的条目控件*/
    private LinearLayout mLayout;

    /*确认按钮*/
    private Button mBtnSure;

    /*popupWindow是否显示*/
    private AtomicBoolean popIsShowing = new AtomicBoolean();
    private ImageView mIvArrow;
    private ComplainInfo complainInfo;
    private int reason;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint_cancle);

        setHeader();

        initView();

        initData();

        setListener();
    }


    /**
     * 设置标题栏
     */
    private void setHeader() {
        setTitle(R.string.main_home_complaint_btn_cancle);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //取消投诉原因
        mTvReason = (TextView) findViewById(R.id.tv_complaint_cancle_reason);
        //条目控件
        mLayout = (LinearLayout) findViewById(R.id.ll_complaint_cancle);
        //确认
        mBtnSure = (Button) findViewById(R.id.btn_complaint_cancle_sure);
        //箭头
        mIvArrow = (ImageView) findViewById(R.id.iv_arrow);

        popIsShowing.set(false);
    }

    private void initData() {

        //获取取消订单的id
        Intent intent = getIntent();
        complainInfo = (ComplainInfo) intent.getSerializableExtra(Constants.COMPLAIN_INFO_KEY);

        //取消投诉的Res
        reasons = new int[]{R.string.select_complaint_cancle_reason_one, R.string.select_complaint_cancle_reason_two, R.string.select_complaint_cancle_reason_three};

    }

    /**
     * 设置监听器
     */
    private void setListener() {

        //取消投诉的条目点击事件
        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (popupWindow == null) {
                    //弹出PopupWindow
                    initListView();
                }

                if (popIsShowing.get()) {
                    popIsShowing.set(false);
                } else {
                    popupWindow.showAsDropDown(mLayout, 0, 0);
                    mIvArrow.setImageResource(R.drawable.online_arrow);
                    popIsShowing.set(true);
                }

            }
        });

        //确定按钮的点击
        mBtnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String reasonStr = mTvReason.getText().toString().trim();
                if(Tools.isEmpty(reasonStr)){
                    MyToast.show(ComplaintCancleActivity.this,getResources().getString(R.string.select_complaint_cancle_reason));
                }else{
                    loadingDialog = DialogFactory.getLoadingDialog(ComplaintCancleActivity.this);
                    DialogFactory.showDialog(loadingDialog);
                    Map<String,Object> maps = new HashMap<String, Object>();
                    maps.put("userid", CacheUtils.getIntData(ComplaintCancleActivity.this,Constants.LOGIN_UID,0));
                    maps.put("id",complainInfo.id);
                    maps.put("reason",reason);
                    maps.put("remarks","");
                    MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_CANCEL_COMPLAIN_URL, maps, MyHttp.POST, ComplaintCancleActivity.this, false, null, new SimplePaserJsonCallback() {
                        @Override
                        public void onVerifyIsNull() {

                            DialogFactory.dismissLoadingDialog(loadingDialog);
                            setResult(RESULT_OK);
                            finish();
                        }

                        @Override
                        public void onVerifySuccess(String dataRaw, boolean isCache) {

                        }

                        @Override
                        public void onErrorCodeError(BaseEntity.MyError mError) {
                            super.onErrorCodeError(mError);
                            DialogFactory.dismissLoadingDialog(loadingDialog);
                        }
                    });
                }

            }
        });

    }

    /**
     * 初始化ListView
     */
    private void initListView() {

        //创建popupWindow中的ListView
        ListView listView = new ListView(this);

        //设置无分割线
        listView.setDivider(null);

        //设置Adapter
        MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

//        // 创建PopupWindow
        popupWindow = new PopupWindow(getApplicationContext());

        // 设置宽度(帐户容器的宽度)
        popupWindow.setWidth(mLayout.getWidth());
        popupWindow.setHeight(layoutParams.height);

        // 设置布局
        popupWindow.setContentView(listView);

        // 设置超出pop范围点击关闭PoppuWindow
        popupWindow.setOutsideTouchable(true);

        popupWindow.setFocusable(true);

        //设置背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popIsShowing.set(false);
                mIvArrow.setImageResource(R.drawable.online_arrow_down);
            }
        });

        // 监听Listview的条目点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                int reasonInput = reasons[position];
                mTvReason.setText(reasonInput);
                reason = position+1;
                popupWindow.dismiss();
            }
        });

    }

    /**
     * listview的adapter
     */
    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return reasons.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {

            convertView = View.inflate(ComplaintCancleActivity.this, R.layout.item_complaint_cancle, null);

            TextView tv_reason = (TextView) convertView.findViewById(R.id.tv_item_cancle_reason);

            View viewLine = convertView.findViewById(R.id.view);

            if (position == 0) {
                viewLine.setVisibility(View.GONE);
            } else {
                viewLine.setVisibility(View.VISIBLE);
            }

            tv_reason.setText(reasons[position]);

            return convertView;
        }

    }

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return true;
    }


}
